package com.bdqn.vueboot.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
@RestController
public class GloablException {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exception(SQLIntegrityConstraintViolationException ex) {
        log.error(ex.getMessage());
        if (ex.getMessage().contains("Duplicate entry")) {
            String[] as = ex.getMessage().split(" ");
            return R.error(as[2] + "已存在,请重新输入！");
        }

        return R.error("错误！！！");

    }

    @ExceptionHandler(CustomException.class)
    public R<String> exceptionCustom(CustomException ex) {
        log.error(ex.getMessage());
        return R.error(ex.getMessage());

    }
}
